<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{

    /*
      前台页面展示
    */
    public function getIndex()
    {
      // 查询分类
      $cates = DB::table('dota_cate')->where('status',1)->Where('pid', '0')->orderby('id','ASC')->get();
      // 查询分类的id
      /*foreach($cates as $k=>$v){
        $news[] = DB::table('dota_news')->where('pid',$v->id)->orderby('time','DESC')->get();
      }*/
      // dd($cates);
      // 根据是否置顶分类查询新闻
      // 普通
      $news = DB::table('dota_news')->where('status',2)->orderby('pid','DESC')->orderby('time','DESC')->get();
      // 置顶
      $tnews = DB::table('dota_news')->where('status',1)->orderby('pid','DESC')->orderby('time','DESC')->get();
      // dd($news);
      // 解析模板
      return view('home.index',['cates'=>$cates,'news'=>$news,'tnews'=>$tnews]);
    }

    /*
      新闻列表页
    */
    public function getLists($id)
    {
      // dd($id);
      // 查询该分类名称
      $cates = DB::table('dota_cate')->where('id',$id)->first();
      // 获取最新添加的新闻
      $nnews = DB::table('dota_news')->where('status','=',2)->where('pid','=',$id)->orderby('time','DESC')->first();
      // 查询本分类下的未置顶新闻(除了上调查询的)
      $news = DB::table('dota_news')->where('status','=',2)->where('pid','=',$id)->where('id','!=',$nnews->id)->orderby('time','DESC')->get();
      // 查询本分类下的置顶新闻
      $tnews = DB::table('dota_news')->where('status','=',1)->where('pid','=',$id)->orderby('time','DESC')->get();
     
      // 解析模板
      return view('home.news.newslist',['cates'=>$cates,'news'=>$news,'tnews'=>$tnews,'nnews'=>$nnews]);
    }

    /*
      新闻详情页
    */
    public function getNews($id)
    {
      // 查询该新闻内容
      $news = DB::table('dota_news')->where('id',$id)->first();
      // 查询分类名称
      $cates = DB::table('dota_cate')->where('id',$news->pid)->first();
      // 解析模板
      return view('home.news.news',['news'=>$news,'cates'=>$cates]);
    }
}
